Skip to content

Experiments: Fix every toggle reading as off with stale saved keys - #82072

Closed
jasmussen wants to merge 1 commit into
trunkfrom
fix/experiments-screen-stale-keys
Closed

jasmussen wants to merge 1 commit into
trunkfrom
fix/experiments-screen-stale-keys

Conversation

@jasmussen

Copy link
Copy Markdown
Contributor

What?

Fair warning: This PR is largely Claude-authored as a fix to an issue I experience in an unrelated session. Which is that if an experiment key exists but isn't registered as an experiment.

Therefore honestly success could be a very quick glance at this PR and a dev saying "nah, it's fine". But in case it's helpful, I'll pass it over to Claude.


The Experiments screen rendered every toggle as "off" whenever the saved gutenberg-experiments option contained a key that is not a currently registered experiment. This is easy to hit in practice: switching between branches that register different experiments leaves stale keys behind.

WP_REST_Settings_Controller::get_registered_options() runs each registered schema through rest_default_additional_properties_to_false(), which stamps additionalProperties => false onto any object schema that does not set it. prepare_value() then validates the stored option against that schema and returns null when validation fails, so one stale key blanked out the whole option in GET /wp/v2/settings and the screen had no values left to render.

The failure was both closed and silent, which is what made it worth fixing rather than working around: the user saw a plausible-looking screen that was simply wrong, and toggling anything on would then write back a value that dropped their other settings.

Set additionalProperties to true so the stored value survives validation intact. Unknown keys are preserved rather than stripped, because they usually belong to a branch the user can switch back to, and discarding them would silently lose settings they never asked to change. Since the schema now accepts arbitrary properties on write, a sanitize_callback normalises what gets persisted to a flat map of booleans.

On the client, a null value is now distinguished from "nothing is enabled". When the stored value cannot be read the page shows an error notice instead of the form, because rendering toggles from an unknown state would let a single click overwrite whatever is really stored. That closes the silent failure for any remaining cause, not just stale keys.

Note that phpunit/bootstrap.php has been seeding this option with four keys that are no longer registered, so the test fixture was hitting this too.

Use of AI Tools

Claude Opus 5.

The Experiments screen rendered every toggle as "off" whenever the saved
`gutenberg-experiments` option contained a key that is not a currently
registered experiment. This is easy to hit in practice: switching between
branches that register different experiments leaves stale keys behind.

`WP_REST_Settings_Controller::get_registered_options()` runs each registered
schema through `rest_default_additional_properties_to_false()`, which stamps
`additionalProperties => false` onto any object schema that does not set it.
`prepare_value()` then validates the stored option against that schema and
returns null when validation fails, so one stale key blanked out the whole
option in `GET /wp/v2/settings` and the screen had no values left to render.

The failure was both closed and silent, which is what made it worth fixing
rather than working around: the user saw a plausible-looking screen that was
simply wrong, and toggling anything on would then write back a value that
dropped their other settings.

Set `additionalProperties` to true so the stored value survives validation
intact. Unknown keys are preserved rather than stripped, because they usually
belong to a branch the user can switch back to, and discarding them would
silently lose settings they never asked to change. Since the schema now
accepts arbitrary properties on write, a `sanitize_callback` normalises what
gets persisted to a flat map of booleans.

On the client, a null value is now distinguished from "nothing is enabled".
When the stored value cannot be read the page shows an error notice instead
of the form, because rendering toggles from an unknown state would let a
single click overwrite whatever is really stored. That closes the silent
failure for any remaining cause, not just stale keys.

Note that phpunit/bootstrap.php has been seeding this option with four keys
that are no longer registered, so the test fixture was hitting this too.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@jasmussen jasmussen added the [Type] Enhancement A suggestion for improvement. label Aug 26, 2026
@github-actions

Copy link
Copy Markdown

Size Change: 0 B

Total Size: 7.91 MB

compressed-size-action

@t-hamano t-hamano added the Gutenberg Plugin Issues or PRs related to Gutenberg Plugin management related efforts label Aug 26, 2026
@jasmussen

Copy link
Copy Markdown
Contributor Author

Closing this one for lack of interest. We can always resurrect if someone wants to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Gutenberg Plugin Issues or PRs related to Gutenberg Plugin management related efforts [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants